<template>
  <StudentLayout>
    <el-button type="primary" :icon="Plus" @click="addSdeptDig()">新增院系</el-button>
    <el-table :data="sdeptData" style="width: 100%">
      <el-table-column prop="name" label="院系名称" />
      <el-table-column prop="description" label="院系描述" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="addSdeptDig(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="确认删除吗?"
            @confirm="delSdeptSub(scope.row)"
          >
            <template #reference>
              <el-button
                size="small"
                type="danger"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </StudentLayout>
  <el-dialog v-model="dialogFormVisible" :title="isEdit?'编辑院系':'新增院系'"
    width="500">
    <el-form :model="form" @submit.native.prevent>
      <el-form-item label="院系名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="院系描述">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="addSdeptSub()">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import StudentLayout from '@/layouts/StudentLayout.vue'
import  { getSdeptList, addSdept, editSdept, delSdept } from '@/composables/school'

const dialogFormVisible = ref(false)
const isEdit = ref(false)
const editID = ref()
let form = reactive({
  name: '',
  description: ''
})

const delSdeptSub = async (row) => {
  const params = {
    _id: row._id
  }
  await delSdept(params).then(res => {
      changeSdept()
      ElMessage({
        message: '部门删除成功',
        type: 'success',
      })
  })
}

const addSdeptDig = async (index, row) => {
  dialogFormVisible.value = true
  form.name = '' 
  form.description = ''
  isEdit.value = false
  if (row) {
    isEdit.value = true
    form.name = row.name 
    form.description = row.description
    editID.value = row._id
  }
}
const addSdeptSub = async () => {
  if (isEdit.value){
    form._id = editID.value
    await editSdept(form).then(res => {
      dialogFormVisible.value = false
      changeSdept()
      ElMessage({
        message: '部门编辑成功',
        type: 'success',
      })
    })
  } else {
    await addSdept(form).then(res => {
      dialogFormVisible.value = false
      changeSdept()
      ElMessage({
        message: '部门添加成功',
        type: 'success',
      })
    })
  }
}
const listAjax = () => {
  return getSdeptList().then(res => {
    return res.data
  })
}
const changeSdept = async () => {
  sdeptData.value = await listAjax()
}
const {data: sdeptData} = await useAsyncData('sdept', () => {
  return listAjax()
})
</script>
<style scoped>

</style>